package com.cx.base.day13_io.io_09_byteArrayInputOutStream.toCopyByteArray;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;

/**
 * 背景：数组有时需要复制，有人说直接byte[] bytes_new = bytes_origin 不就ok了，麻烦看清楚，这是引用地址复制
 * 常规方法是新创一个数组然后把原数组元素逐个复制到新数组
 * ByteArrayInputStream：虽然是流对象，但本身并不涉及硬件io，就是单纯的数组复制功能的封装，也不需要释放资源
 * T[] copyArrays = Arrays.copyOf(T[] original, int newLength) 或者 System.arraycopy(Object src,int srcPos,Object dest, int destPos,int length)
 * 可以完成新数组的copy,但是新数组的元素仍是旧数组中元素
 *
 * @Author: robert.song
 * @Date: Created in 2022/5/25
 */
public class ByteArrayInputByCopy {

    public static void main(String[] args) throws IOException {
        byte[] bytes = new byte[1024];
        System.out.println(2147483647 + 1);
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) (i % 128);
        }
//        如上一份字符数组，如果需要复制一份新数组，ByteArrayInputStream是推荐工具
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        byte[] bytes_new = new byte[bytes.length];
        bais.read(bytes_new);
        for (byte b : bytes_new) System.out.println(b);

        Integer integer1 = new Integer(1);
        Integer integer2 = new Integer(1);
        Integer integer3 = Integer.valueOf(1);
        Integer integer4 = Integer.valueOf(1);
        System.out.println(integer1 == integer2);
        System.out.println(integer3 == integer4);
        String str1 = "hello";
        String str2 = new String("hello1");
        String str3 = new String("hello2");
        String str4 = new String("hello3");
        String[] strs = {str2, str3, str4};
        String[] copys = Arrays.copyOf(strs, strs.length);
        System.out.println(strs[0]);
        System.out.println(copys[0]);
        System.out.println(strs[0] == copys[0]);
    }
}
